﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.IO;
using System.Xml;

namespace TemplateH4.admin
{
    public partial class QLMailServer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //XmlDocument doc = new XmlDocument();
            //doc.Load(Server.MapPath("~/web.config"));

            //XmlNode root = doc.DocumentElement;

            //txtmailserver.Text = root.SelectSingleNode("system.net").SelectSingleNode("mailSettings").SelectSingleNode("smtp").SelectSingleNode("network").Attributes["host"].Value;
            //txtpassword.Text = root.SelectSingleNode("system.net").SelectSingleNode("mailSettings").SelectSingleNode("smtp").SelectSingleNode("network").Attributes["password"].Value;
            //txtusername.Text = root.SelectSingleNode("system.net").SelectSingleNode("mailSettings").SelectSingleNode("smtp").SelectSingleNode("network").Attributes["userName"].Value;
            //txtserverport.Text = root.SelectSingleNode("system.net").SelectSingleNode("mailSettings").SelectSingleNode("smtp").SelectSingleNode("network").Attributes["port"].Value;
            //txtmailfrom.Text = root.SelectSingleNode("system.net").SelectSingleNode("mailSettings").SelectSingleNode("smtp").Attributes["from"].Value;



            //elements = doc.SelectNodes("//configuration/system.net/mailSettings/smtp");
            //element = (XmlElement)elements[0];
            //String from = element.GetAttribute("from");
            //txtmailfrom.Text = from;
            //txtmailserver.Text = host;
            //txtpassword.Text = password;
            //txtserverport.Text = port;
            //txtusername.Text = "xuannam@gmail.com";
            //txtmailfrom.Text = "xuannam@gmail.com";
            //txtmailserver.Text = "localhost";
            //txtpassword.Text = "123";
            //txtserverport.Text = "25";


            //XmlTextWriter writer = new XmlTextWriter(Server.MapPath("~/web.config"), null);
            //writer.Formatting = Formatting.Indented;
            //doc.Save(writer);
            if (!IsPostBack)
            {

                XmlDocument doc = new XmlDocument();
                doc.Load(Server.MapPath("~/config.xml"));

                XmlNode root = doc.DocumentElement;
                XmlNode smtp = root.SelectSingleNode("smtp");
                txtmailserver.Text = smtp.SelectSingleNode("mailserver").ChildNodes[0].Value;
                txtserverport.Text = smtp.SelectSingleNode("port").ChildNodes[0].Value;
                txtmailfrom.Text = smtp.SelectSingleNode("from").ChildNodes[0].Value;
                txtusername.Text = smtp.SelectSingleNode("username").ChildNodes[0].Value;
                txtpassword.Text = smtp.SelectSingleNode("password").ChildNodes[0].Value;
                doc.Save(Server.MapPath("~/config.xml"));
            }

            

            
        }

        protected void btnluu_Click(object sender, EventArgs e)
        {
            //<system.net>
            //  <mailSettings>
            //    <smtp from="xuannam@gmail.com">
            //      <network host="localhost" password="" userName="" />
            //    </smtp>
            //  </mailSettings>
            //</system.net>

            
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("~/web.config"));

            XmlNodeList elements = doc.SelectNodes("//configuration/system.net/mailSettings/smtp/network");
            XmlElement element = (XmlElement)elements[0];
            String host = element.GetAttribute("host");
            String xPathExpression = "//configuration/system.net/mailSettings/smtp/network[@host='" + host + "']";
            XmlElement docItem = (XmlElement)doc.SelectSingleNode(xPathExpression);
            docItem.SetAttribute("host", txtmailserver.Text);
            docItem.SetAttribute("userName", txtusername.Text);
            docItem.SetAttribute("password", txtpassword.Text);
            docItem.SetAttribute("port", txtserverport.Text);


            elements = doc.SelectNodes("//configuration/system.net/mailSettings/smtp");
            element = (XmlElement)elements[0];
            String from = element.GetAttribute("from");
            xPathExpression = "//configuration/system.net/mailSettings/smtp[@from='" + from + "']";
            element.SetAttribute("from", txtmailfrom.Text);


            XmlTextWriter writer = new XmlTextWriter(Server.MapPath("~/web.config"), null);
            writer.Formatting = Formatting.Indented;
            doc.Save(writer);


            doc.Load(Server.MapPath("~/config.xml"));
            XmlNode root = doc.DocumentElement;
            //XmlNode node = root.SelectSingleNode("logo").ChildNodes[0];
            XmlNode old = root.SelectSingleNode("smtp");
            XmlElement newsmtp = doc.CreateElement("smtp");
            XmlElement mailserver = doc.CreateElement("mailserver");
            XmlElement port = doc.CreateElement("port");
            XmlElement mailfrom = doc.CreateElement("from");
            XmlElement username = doc.CreateElement("username");
            XmlElement password = doc.CreateElement("password");

            mailserver.InnerText = txtmailserver.Text;
            port.InnerText = txtserverport.Text;
            mailfrom.InnerText = txtmailfrom.Text;
            username.InnerText = txtusername.Text;
            password.InnerText = txtpassword.Text;

            newsmtp.AppendChild(mailserver);
            newsmtp.AppendChild(port);
            newsmtp.AppendChild(mailfrom);
            newsmtp.AppendChild(username);
            newsmtp.AppendChild(password);




            root.ReplaceChild(newsmtp, old);


            writer = new XmlTextWriter(Server.MapPath("~/config.xml"), null);
            writer.Formatting = Formatting.Indented;
            doc.Save(writer);

        }
    }
}

